$(()=>{
    let group="g1";
    let count;
    let voted = [
        "{COOKIE_voted-g1}",
        "{COOKIE_voted-g2}",
        "{COOKIE_voted-g3}"
    ];
    if (role!=="teacher"){
        $("#clear").remove();
    }
    $("#g1").click();
    getVote();
    $("div.select div.item").click(function (){
        $("div.select div.item").removeClass("active");
        $("div.vote div.item").removeClass("active");
        $(this).addClass("active");
        for (let i=1;i<4;i++){
            group = this.id;
            console.log(group);
            getVote();
            $(`img#img${i}`).attr("src",`{LINK_img}?tag=${this.id}i${i}`);
            $(`#check${i}`)[0].checked=false;
        }
    });
    $("div.vote div.item").click(function () {
        $("div.vote div.item").removeClass("active");
        $(this).addClass("active");
        console.log(this.id);
        $(`#chec${this.id}`).click();
    });
    $("#submit").click(()=>{
        let result = [];
        let flag = 0;
        for (let i=0;i<3;i++){
            result[i] = $(`#check${i+1}`)[0].checked?1:0;
            if (result[i]===1){
                flag=1;
            }
        }
        if (flag===0){
            // alert("至少选择一个作品");
            swal("投票失败","至少选择一个作品","error",{timer:2000});
            return;
        }

        // result = "[" + result.toString() + "]";
        // console.log(result.toString());
        $.post("{LINK_sendVote}",{
            group: group,
            result: result.toString()
        },text=>{
            console.log(text);
            let data = JSON.parse(text);
            if (data.status){
                // alert("投票成功");
                swal("投票成功","","success",{timer:2000});
                $("div.vote div.item").removeClass("active");
            }else {
                // alert("投票失败："+data.msg);
                swal("投票失败",data.msg,"error",{timer:2000});
            }
        });
    });
    // $("#clear").click(()=>{
    //     if (confirm("是否清空投票结果，操作不可逆！")) {
    //         $.post("{LINK_clearVote}", {}, text => {
    //             let data = JSON.parse(text);
    //             if (data.status) {
    //                 swal("清空成功","","success",{timer:2000});
    //                 alert("清空成功");
    //             }else{
    //                 swal("清空失败",data.msg,"error",{timer:2000});
    //                 // alert("清空失败："+data.msg);
    //             }
    //         });
    //     }
    // });
    function getVote(){
        $.post("{LINK_getVote}",{
            group: group
        },text=>{
            let data = JSON.parse(text);
            for (let i=0;i<3;i++){
                $(`#result${i+1}`).text(data[i]);
            }
        });
    }

    counter();
    function counter(){
        $.get("{LINK_getCounter}",text=>{
            if (count!==text){
                count=text;
                getVote();
            }
            // setTimeout(counter,500);
            counter();
        });
    }
});